package com.hi.hiparty.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hi.hiparty.entity.Post;
import com.hi.hiparty.entity.Post.PostContentType;
import com.hi.hiparty.manager.PartyManager;
import com.hi.hiparty.manager.PostManager;
import com.hi.hiparty.util.JSONResponseHelper;
import com.hi.hiparty.util.WebUtil;

@Controller
public class PostController {

	private static final Log logger = LogFactory.getLog(PostController.class);

	@Autowired
	private PartyManager partyManager;

	@Autowired
	private PostManager postManager;

	@RequestMapping(value = "/mobileapi/post/publish")
	public void publish(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int contentType = WebUtil.getIntPara(request, "contenttype");
		String partyid = WebUtil.getStringPara(request, "partyid");
		String userid = WebUtil.getStringPara(request, "userid");
		JSONObject json = new JSONObject();
		switch (contentType) {
		case PostContentType.Text:
			String text = WebUtil.getStringPara(request, "text");
			json.put("body", text);
			break;
		case PostContentType.Image:
			String imageurl = WebUtil.getStringPara(request, "imageurl");
			json.put("image_url", imageurl);
			break;
		case PostContentType.Address:
			String address = WebUtil.getStringPara(request, "address");
			String lonlat = WebUtil.getStringPara(request, "lonlat");
			json.put("address", address);
			json.put("lonlat", lonlat);
			break;
		default:
			break;
		}
		Post post = new Post();
		post.setPartyid(partyid);
		post.setUserid(userid);
		post.setContentType(contentType);
		post.setContent(json.toString());
		post.setCreateTime(new Date());
		postManager.insert(post);

		JSONObject result = new JSONObject();
		result.put("success", 1);
		result.put("saved_post", post.toJSON());
		JSONResponseHelper.sendJSONResult(response, result, null);
	}

	@RequestMapping(value = "/mobileapi/post/list/{partyid}/{start}/{size}")
	public void list(@PathVariable("partyid") String partyid, @PathVariable("start") int start,
			@PathVariable("size") int size, HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Post> list = postManager.list(partyid, start, size);
		JSONObject result = new JSONObject();
		JSONArray array = new JSONArray();
		if (null != list && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				array.add(list.get(i).toJSON());
			}
		}
		result.put("success", 1);
		result.put("list", array);
		JSONResponseHelper.sendJSONResult(response, result, null);
	}

}
